const fs = require('fs');
const path = require('path');

// 获取环境变量
const env = process.env.APP_ENV || 'local';

// 环境配置映射
// VITE_APP_GRAFANA_URL: Grafana监控面板完整URL（包含所有参数）
const envConfig = {
  local: {
    VITE_APP_TITLE: 'Seatunnel Admin (Local)',
    VITE_APP_ENV: 'local',
    VITE_APP_BASE_API: 'http://localhost:9000/api',
    VITE_APP_GRAFANA_URL: 'https://shennong-grafana-ft-stage.cdreader.vip/d/bdx1j097hmku8d/seatunnel-e79b91-e68ea7-e99da2-e69dbf?from=now-5m&to=now&var-cluster=seatunnel-ccen&var-instance=$__all&refresh=5s&kiosk=tv&var-id=$__all'
  },
  dev: {
    VITE_APP_TITLE: 'Seatunnel Admin (Dev)',
    VITE_APP_ENV: 'dev',
    VITE_APP_BASE_API: 'https://seatunnel-admin-server-none-new-dev.changdu.ltd/api',
    VITE_APP_GRAFANA_URL: 'https://shennong-grafana-ft-stage.cdreader.vip/d/bdx1j097hmku8d/seatunnel-e79b91-e68ea7-e99da2-e69dbf?from=now-5m&to=now&var-cluster=seatunnel-ccen&var-instance=$__all&refresh=5s&kiosk=tv&var-id=$__all'
  },
  test: {
    VITE_APP_TITLE: 'Seatunnel Admin (Test)',
    VITE_APP_ENV: 'test',
    VITE_APP_BASE_API: 'https://seatunnel-admin-server-none-new-test.changdu.ltd/api',
    VITE_APP_GRAFANA_URL: 'https://shennong-grafana-ft-stage.cdreader.vip/d/bdx1j097hmku8d/seatunnel-e79b91-e68ea7-e99da2-e69dbf?from=now-5m&to=now&var-cluster=seatunnel-ccen&var-instance=$__all&refresh=5s&kiosk=tv&var-id=$__all'
  },
  prod: {
    VITE_APP_TITLE: 'Seatunnel Admin (Prod)',
    VITE_APP_ENV: 'prod',
    VITE_APP_BASE_API: 'https://seatunnel-admin-server-none-prod.changdu.vip/api',
    VITE_APP_GRAFANA_URL: 'https://shennong-grafana-ft-stage.cdreader.vip/d/bdx1j097hmku8d/seatunnel-e79b91-e68ea7-e99da2-e69dbf?from=now-5m&to=now&var-cluster=seatunnel-ccen&var-instance=$__all&refresh=5s&kiosk=tv&var-id=$__all'
  }
};

// 生成环境配置文件内容
const envContent = Object.entries(envConfig[env])
  .map(([key, value]) => `${key}=${value}`)
  .join('\n');

// 写入 .env 文件
fs.writeFileSync(path.join(__dirname, '.env'), envContent);

console.log(`Environment configuration for ${env} has been created.`);
console.log('Environment variables:', envConfig[env]); 